package com.fenda.myviewpager

import android.animation.ObjectAnimator
import androidx.lifecycle.ViewModelProvider
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import kotlin.random.Random

class ScaleFragment : Fragment() {
    private var imageView: ImageView? = null

    companion object {
        fun newInstance() = ScaleFragment()
    }

    private lateinit var viewModel: ScaleViewModel

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.scale_fragment, container, false)
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        viewModel = ViewModelProvider(this).get(ScaleViewModel::class.java)
        imageView = view?.findViewById(R.id.scale)
        imageView?.setOnClickListener {
            val factor: Float = if (Random.nextBoolean()) 0.1f else -0.1f
            ObjectAnimator.ofFloat(it,"scaleX",it.scaleX+ factor).start()
        }
    }

}